home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 5663 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.7 KB

  1. Path: nntp.teleport.com!usenet
  2. From: GHouck <hksys@teleport.com>
  3. Newsgroups: comp.lang.c
  4. Subject: Re: accessing structures (newbie question)
  5. Date: 20 Feb 1996 10:24:03 GMT
  6. Organization: systems hk
  7. Message-ID: <4gc7g3$dm7@maureen.teleport.com>
  8. References: <4g8gic$o6u@news1.sunbelt.net>
  9. NNTP-Posting-Host: ip-pdx04-22.teleport.com
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=us-ascii
  12. Content-Transfer-Encoding: 7bit
  13. X-Mailer: Mozilla 1.22 (Windows; I; 32bit)
  14.  
  15. dking@SunBelt.Net wrote:
  16. >struct picture *photos;
  17. >photos = (struct picture *)malloc(1000 * sizeof(picture));
  18. >
  19. >now I try to store data into structures...
  20. >    for (x=0;x<num_of_files;x++)
  21. >    {
  22. >        strncpy(photos->filename, filelist[x],12);
  23. >        strncpy(photos->diskname, inputdisk,12);
  24. >        strncpy(photos->indexname, inputindex,12);
  25. >    }
  26. >for (x=0;x<num_of_files;x++)
  27. >{
  28. >printf("%sdisk%sindex%s\n",photos->filename,photos->diskname,photos->indexname
  29. >Obviously I'm pointing to the first of the 1000 structures the entire time. 
  30. >Now for the 60,000 dollar question: HOW do I access the rest of the 
  31. >structures? My tutuorial doesnt have any examples of accessing  MALLOCed 
  32. >structures. The one example on structures defines it as an array (struct 
  33. >picture *photos[x] ) but 1000 elements is too large an array and the example 
  34. >with malloc uses floating point data accessed by using (photos[x]) which 
  35. >doesnt work either with the strnccpy or with the -> operator.
  36. >
  37. Daryl,
  38.  
  39. Just can dereferemce the structures with a pointer offset or a subscript,
  40. as below:
  41.  
  42.   (photos+x)->filename, (photos+x)->diskname, ...
  43.     or
  44.   photos[x].filename, photos[x].diskname, ...
  45.  
  46. The 'x' offset/subscript increments the sizeof(photos) each time.
  47.  
  48. Yours, Geoff Houck
  49.  
  50.